import Reqwest from '../../utils/Reqwest';
import {base, assign, redirect} from '../base/Base';
import {ResponseDto} from '../base/BaseDTO';
import {SudokuBean} from './SudokuBean';
import {SudokuIconBean} from './SudokuIconBean';

declare let GLOBAL: any;

interface categoryBean {
    type: number;
    name: string;
}

export class SudokuController {

    path = `${GLOBAL.baseApiUrl}/thirdpart`;

    /**
     * 新增九宫格
     * @param params
     * @param {{}} options
     * @returns {Promise<ResponseDto<any>>}
     */
    addSudoku(params: {
        appName: string;
        platform: string;
        areaIds: Array<number>;
        appServiceJson: Array<any>;
    }, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/addAppServices.jhtml?areaIds=${params.areaIds.join(',')}`,
            method: 'post',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(params.appServiceJson) || [],
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }


    /**
     * 查看九宫格.
     *
     * @return
     */
    checkSudoku(params: {
        appName: string;
        platform: string;
        areaId: number;
        category: number;
    }, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/viewAppServices.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * 九宫格列表.
     *
     * @return
     */
    searchSudoku(params?: any, options = {}): Promise<any> {
        return new Reqwest(assign({
            url: `${this.path}/getAreaAppServiceSummary.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * 九宫格选项
     */
    getSudokuOptions(params?: {
        appName: string;
        platform: string;
    }, options = {}): Promise<ResponseDto<SudokuIconBean>> {
        return new Reqwest(assign({
            url: `${this.path}/viewAppServiceOptions.jhtml`,
            method: 'get',
            type: 'json',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * 九宫格类型
     */
    getSudokuCategory(params?, options = {}): Promise<ResponseDto<categoryBean>> {
        return Promise.resolve({
            success: true,
            result: [{
                type: 0,
                name: '九宫格图标'
            }, {
                type: 1,
                name: '底部菜单栏'
            }],
            message: ''
        })
    }

}